import { createRouter, createWebHashHistory } from 'vue-router'
import { useStore } from '@/store/index'

const routes = [
  {
    path: '/alive',
    name: 'alive',
    component: () => import('@/views/Keepalive.vue')
  },
  {
    path: '/teleport',
    name: 'teleport',
    component: () => import('@/views/Teleport.vue')
  },
  {
    path: '/state',
    name: 'state',
    component: () => import('@/views/UsePiniaState.vue')
  },
  {
    path: '/getter',
    name: 'getter',
    component: () => import('@/views/UsePiniaGetter.vue')
  },
  {
    path: '/action',
    name: 'action',
    component: () => import('@/views/UsePiniaAction.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 根据导入的顺序，这里导入会失败
// const store = useStore()
// console.log(store)

router.beforeEach((to, from, next) => {
  // 我们应该在这里调用 pinia，因为路由器在之后开始导航，路由已安装，pinia也将安装
  const store = useStore()
  console.log(store.count)
  next()
})

export default router